/**
 * 202. 快乐数
 * https://leetcode.cn/problems/happy-number/description/
 */
class Solution2 {
    public boolean isHappy(int n) {
        int fast = n, slow = n;
        do {
            slow = bitSum(slow);
            fast = bitSum(bitSum(fast));
        } while (slow != fast);
        return slow == 1;
    }

    // 得到某个数每一位的平方和
    public int bitSum(int n) {
        int ret = 0;
        while(n > 0) {
            int tmp = n % 10;
            ret += tmp * tmp;
            n /= 10;
        }
        return ret;
    }
}